package com.ailk.emall.busi.pmloginauth.controller;

import java.util.Random;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.gcf.api.emall.pmloginauth.interfaces.IPmLoginAuthDubboSV;
import com.ai.gcf.api.emall.pmloginauth.param.LoginRequest;
import com.ai.gcf.api.emall.pmloginauth.param.LoginResponse;
import com.ailk.emall.busi.base.controller.BaseController;
import com.ailk.emall.busi.pmloginauth.model.SysOpBean;
import com.ailk.emall.constants.ExcepConstants;
import com.ailk.emall.constants.SessionConstants;
import com.ailk.emall.exception.BusiException;
import com.ailk.emall.utils.MD5Util;
import com.ailk.emall.utils.SessionUtil;
import com.alibaba.fastjson.JSONObject;

/** 
 * 登陆相关
 * @author zhaowg3
 * @Date 2017年1月6日
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController{
	@Autowired
	private IPmLoginAuthDubboSV iPmLoginAuthDubboSV;
	/**
	 * 跳转到登陆界面
	 * @return
	 * @author zhaowg3
	 * @Date 2017年1月6日
	 */
	@RequestMapping("/login")
	public String index(){
		return "signin";
	}
	/**
	 * 登陆
	 * @return
	 * @author zhaowg3
	 * @Date 2017年1月6日
	 */
	@ResponseBody
	@RequestMapping("logining")
	public JSONObject logining(@RequestParam String username,@RequestParam String password,@RequestParam String code,@RequestParam String timestamp)throws Exception{
		try{
			//判断验证码是否正确
			this.validateCodeRight(code, timestamp);
			session.removeAttribute(SessionConstants.Login.VERIFY_CODE+timestamp);
			LoginRequest loginRequest = new LoginRequest();
			loginRequest.setUserName(username);
			//生成6位随机码
			String token = RandomStringUtils.random(6);
			loginRequest.setToken(token);
			//对密码进行加密，然后使用随机码对加密后密码进行二次加密
			String pwd = MD5Util.MD5((MD5Util.MD5(password)+token));
			loginRequest.setPassWord(pwd);
			LoginResponse loginResponse = iPmLoginAuthDubboSV.login(loginRequest );
			if(!loginResponse.getResponseHeader().getResultCode().equals(ExcepConstants.Special.SUCCESS))
				throw new BusiException(ExcepConstants.Login.USERNAME_ERROR, loginResponse.getResponseHeader().getResultMessage());
			SysOpBean sysOpBean = new SysOpBean();
			sysOpBean.setLoginName(loginResponse.getLoginName());
			sysOpBean.setOpCode(loginResponse.getOpCode());
			sysOpBean.setOpId(loginResponse.getOpId());
			sysOpBean.setOpName(loginResponse.getOpName());
			SessionUtil.setSysOp(session, sysOpBean);
		}catch(BusiException e){
			e.printStackTrace();
			if(e.getCode()==ExcepConstants.Login.VALIDATE_CODE_ERROR)
				return responseFailed("validateCodeError", null);
			else if(e.getCode()==ExcepConstants.Login.USERNAME_ERROR)
				return responseFailed("usernameError", null);
		}
		return responseSuccess("成功", null);
	}
	/**
	 * 验证验证码是否正确
	 * @return
	 * @author zhaowg3
	 * @Date 2017年1月6日
	 */
	private void validateCodeRight(String code,String timestamp) throws Exception{
		//判断验证码是否正确
		String oldCode = (String)session.getAttribute(SessionConstants.Login.VERIFY_CODE+timestamp);
		if(StringUtils.isBlank(oldCode) || !oldCode.equalsIgnoreCase(code))
			throw new BusiException(ExcepConstants.Login.VALIDATE_CODE_ERROR, "验证码不正确");
	}
	/**
	 * 退出登陆
	 * @author zhaowg3
	 * @Date 2017年1月6日
	 */
	@RequestMapping("/logout")
	public String logout(){
		SysOpBean sysOpBean = SessionUtil.getSysOp(session);
		if(sysOpBean != null)
			session.removeAttribute(SessionConstants.Login.SYSOP);
		if(session!=null)
			session.invalidate();
		return "redirect:/login/login";
	}
}
